package edu.jmu.action.sysmanage.log;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import edu.jmu.action.BaseAction;
import edu.jmu.action.privilege.Permission;
import edu.jmu.bean.PageView;
import edu.jmu.bean.system.OperateLog;
import edu.jmu.service.sysmanage.OperateLogService;

@Controller
@Scope(value="prototype")
public class OperateManageAction extends BaseAction<OperateLog>{
	private static final long serialVersionUID = 1L;
	
	@Resource
	private OperateLogService operateLogService;
	private Integer[] delId;
	private String startDate;
	private String endDate;
	
	@Permission(module="sysmanage",privilege="listOperate",desc="展示操作日志列表")
	public String listOperate(){
		pageView = new PageView<OperateLog>(maxresult,currentpage);
		java.util.LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		if (startDate != null || endDate != null ) {
			if( !"".equals(startDate) || !"".equals(endDate)){
				String wherejpql = "o.operateTime >=?1 and o.operateTime <=?2";
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				try {
					pageView.setQueryResult(operateLogService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),wherejpql,new Date[]{dateFormat.parse(startDate),dateFormat.parse(endDate)},orderby));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else{
				pageView.setQueryResult(operateLogService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
			}
		}else{
			pageView.setQueryResult(operateLogService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
		}
		return SUCCESS;
	}
	
	@Permission(module="sysmanage",privilege="deleteOperate",desc="删除操作日志列表")
	public String deleteOperate(){
		operateLogService.delete((Serializable[])delId);
		return SUCCESS;
	}
	
	public void setDelId(Integer[] delId) {
		this.delId = delId;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getStartDate() {
		return startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	
}
